package com.yun2s.tools.db;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 创建表时使用的DTO，和表字段对应
 * @author "fanhm"
 *
 */
public class TableClnDTO {
	
	/**
	 * 建的表名称
	 */
	private String tableName;
	/**
	 * 数据写入成功失败结果
	 */
	private boolean exeResult;

	public String pk = "30";
	
	public String polno = "30";
	
	public BigDecimal amount;
	
	public Date createDate;
	
	
	public TableClnDTO(){}
	
	public TableClnDTO(String tableName){
		this.tableName = tableName;
	}
	
	 

	public boolean isExeResult() {
		return exeResult;
	}

	public void setExeResult(boolean exeResult) {
		this.exeResult = exeResult;
	}

	public String getTableName() {
		return tableName;
	}
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	public String getPk() {
		return pk;
	}
	public void setPk(String pk) {
		this.pk = pk;
	}
	

	public Date getCreateDate() {
		return createDate;
	}
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	public String getPolno() {
		return polno;
	}
	public void setPolno(String polno) {
		this.polno = polno;
	}
	public BigDecimal getAmount() {
		return amount;
	}
	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}
	
	/**
	 * 判断传入的DTO属性名称是否需要加到表的列中
	 * @param clName
	 * @return
	 */
	public boolean isNeedAddToTableCln(Field field){
		if(field.getName().equals("tableName")){
			return false;
		}
		if(field.getName().equals("exeResult")){
			return false;
		}
		return true;
	}
	
	/**
	 * 组装创建表语句  语句 
	 * @return
	 */
	public String initCreateTableSQL()    {
		StringBuffer sb = new StringBuffer();
		sb.append("create table ");
		sb.append(this.getTableName());
		sb.append("(");
		try {
			for(Field field:this.getClass().getDeclaredFields()){
				if(!isNeedAddToTableCln(field)){
					continue;
				}
				if(field.getType().toString().equals("class java.lang.String")){
					sb.append(field.getName() + " varchar(").append(field.get(this).toString())
					  .append(")").append(",");
				}else if(field.getType().toString().equals("class java.math.BigDecimal")){
					sb.append(field.getName() + " numeric(18,2),");
				}else if(field.getType().toString().equals("class java.util.Date")){
					sb.append(field.getName() + " date,");
				}
				
			}
			sb.deleteCharAt(sb.length()-1);
			sb.append(")");
			
			System.out.println(sb.toString()); 
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		return sb.toString();
	}
	
	public String getDarByDateForInsertString(Date date){
		SimpleDateFormat  df = new SimpleDateFormat("yyyy-MM-dd");
		return "date('"+df.format(date)+"')";
	}
	
	/**
	 * 组装insert数据语句 
	 * @return
	 */
	public String  initInsertSQl(){
		StringBuffer sb = new StringBuffer();
		sb.append("insert into ");
		sb.append(this.getTableName());
		sb.append("(");
		for(Field field:this.getClass().getDeclaredFields()){
			if(!isNeedAddToTableCln(field)){
				continue;
			}
			sb.append(field.getName()).append(","); 
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append(")values(");
		try {
			for(Field field:this.getClass().getDeclaredFields()){
				if(!isNeedAddToTableCln(field)){
					continue;
				}
				if(field.getType().toString().equals("class java.lang.String")){
					sb.append("'"+field.get(this)+"'").append(","); 
				}else if(field.getType().toString().equals("class java.math.BigDecimal")){
					sb.append(field.get(this)).append(","); 
				}else if(field.getType().toString().equals("class java.util.Date")){
					sb.append(getDarByDateForInsertString((java.util.Date)field.get(this))).append(","); 
				}
				
			}
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append(")");
		System.out.println(sb.toString());
		return sb.toString();
	}
	
	
	public static void main(String[] args){
		TableClnDTO dto = new TableClnDTO();
		dto.initCreateTableSQL();
	}
}
